package com.htjc.integral.account.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "jfLog")
@Component
public class FtpUtil {
	
	/** FTP主机服务器   */
	public static String ftpHost;
	/** FTP端口   */
	public static int ftpPort;
	/** FTP登录用户名   */
	public static String ftpUserName;
	/** FTP登录密码   */
	public static String ftpPassword;
	
	@Value("${ftp.host}")
	public void setFtpHost(String host) {
		ftpHost = host;
	}
	@Value("${ftp.port}")
	public void setFtpPort(int port) {
		ftpPort = port;
	}
	@Value("${ftp.userName}")
	public void setFtpUserName(String userName) {
		ftpUserName = userName;
	}
	@Value("${ftp.password}")
	public void setFtpPassword(String password) {
		ftpPassword = password;
	}


	/**
     * 获取FTPClient对象
     * 
     * @param ftpHost
     *            FTP主机服务器
     * @param ftpPassword
     *            FTP 登录密码
     * @param ftpUserName
     *            FTP登录用户名
     * @param ftpPort
     *            FTP端口 默认为21
     * @return
     */
    public static FTPClient getFTPClient(String logMsg) {
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient = new FTPClient();
            ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
            ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                log.info(logMsg+"未连接到FTP，用户名或密码错误。");
                ftpClient.disconnect();
            } else {
                log.info(logMsg+"FTP连接成功。");
            }
        } catch (SocketException e) {
            e.printStackTrace();
            log.info(logMsg+"FTP的IP地址可能错误，请正确配置。");
        } catch (IOException e) {
            e.printStackTrace();
            log.info(logMsg+"FTP的端口错误,请正确配置。");
        }
        return ftpClient;
    }

    /**
     * 下载文件
     * 
     * @param ftpHost ftp服务器地址
     * @param ftpUserName anonymous匿名用户登录，不需要密码。administrator指定用户登录
     * @param ftpPassword 指定用户密码
     * @param ftpPort ftp服务员器端口号
     * @param ftpPath  ftp文件存放物理路径
     * @param fileName 文件路径
     * @param input 文件输入流，即从本地服务器读取文件的IO输入流
     */
    public static void downloadFile(String ftpPath,String localPath,String fileName,String logMsg) {
        FTPClient ftpClient = null;
        try {
            ftpClient = getFTPClient(logMsg);
            ftpClient.setControlEncoding("UTF-8"); // 中文支持
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            ftpClient.changeWorkingDirectory(ftpPath);

            File localFile = new File(localPath + File.separatorChar + fileName);
            OutputStream os = new FileOutputStream(localFile);
            ftpClient.retrieveFile(fileName, os);
            os.close();
            ftpClient.logout();

        } catch (FileNotFoundException e) {
            log.error(logMsg+"没有找到" + ftpPath + "文件");
            e.printStackTrace();
        } catch (SocketException e) {
            log.error(logMsg+"连接FTP失败");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            log.error(logMsg+"文件读取错误");
            e.printStackTrace();
        }
    }
    
    /**
     * 上传文件
     * 
     * @param ftpHost ftp服务器地址
     * @param ftpUserName anonymous匿名用户登录，不需要密码。administrator指定用户登录
     * @param ftpPassword 指定用户密码
     * @param ftpPort ftp服务员器端口号
     * @param ftpPath  ftp文件存放物理路径
     * @param fileName 文件路径
     * @param input 文件输入流，即从本地服务器读取文件的IO输入流
     */
    public static void uploadFile(String ftpPath,String fileName,InputStream input,String logMsg){
        FTPClient ftp=null;
        try {
            ftp=getFTPClient(logMsg);
            ftp.changeWorkingDirectory(ftpPath);
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            fileName=new String(fileName.getBytes("GBK"),"iso-8859-1");
            ftp.storeFile(fileName, input);
            ftp.logout();
            log.info(logMsg+"FTP上传成功（{}）",ftpPath+fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
